<?php
session_start();

function check_login() {
    if ( !check_auth()) {
        redirect(BASE_URL .'login.php');
    }
    
}
/**
 * Check user authentication
 *  
 */
function check_auth() {
     
    if (isset($_SESSION['_user_auth'])) {
        return true;
    }
    return false;
}
/**
 * User login
 * 
 * @global ezSQL_mysql $dbp 
 * @param string $username
 * @param string $password 
 */
function do_logon($username, $password) {
    
    global $dbp;
    $username = $dbp->escape($username);
    $password = md5($password);
    
    $query = "SELECT * FROM users WHERE username = '$username' AND password = '$password'";
    $user = $dbp->get_row($query);
  
    if (is_object($user)) {
        $_SESSION['_user_auth'] = 1;
        $_SESSION['user_data'] = $user; 
        
        return true;
    } else {
       return false; 
    }
    
}
function current_user() {
    if (check_auth()) {
        return $_SESSION['user_data'];
    } else {
        return false;
    }
}
/**
 * Logout function
 *
 *
 */
function do_logout()
{
	if (isset( $_SESSION['_user_auth'])) {
		unset( $_SESSION['_user_auth']);
		unset( $_SESSION['user_data']);
                redirect(BASE_URL.'login.php');
	}


}

/**
 * Re
 * @param type $url
 * @param type $permanent 
 */
function redirect($url,$permanent = false)
{
  if($permanent)
  {
    header('HTTP/1.1 301 Moved Permanently');
  }
  header('Location: '.$url);
  exit();
}

/**
 * Function uploud image
 *
 * @return string image name
 */
function upload_image($source, $path, $name = null){

	$is_image =array('jpg', 'gif', 'png', 'jpeg');
        $name = str_replace(' ', '-', $name);
        $name = strtolower($name);

	if(is_array($source)){
		$ext = explode(".", $source['name']);
		$ext = strtolower(end($ext));

		//if upload is image
		if(in_array($ext, $is_image)){

			//upload image
			$upload = new uploader();
			$upload->source = $source;

                        if( !$name ){
                            $name = uniqid().'-'.time();
                        }else{
                            $name = strtolower( $name ) .'-'. time();
                        }

			$upload->setName($name);
			$upload->destDir = $path;
			//start upload
			$upload->upload("");

			if($upload->getError())
                            return false;

			else
                            return $upload->getName();
		}
	}

       return false;

}

/**
 *
 * @global ezSQL_mysql $dbp
 * @param int $brand_id 
 */
function get_phone_brand($brand_id, $limit = 10) {
    global $dbp;
    
    $phones = $dbp->get_results("SELECT * FROM phone WHERE brand_id = {$brand_id} ORDER BY id DESC LIMIT {$limit} ");
    
    if (!empty($phones)) {
        return $phones;
    } 
    return false;
}

function get_option_by_key($key) {
    
    global $dbp;
    $option = $dbp->get_row("SELECT * FROM options WHERE `key` = '{$key}' ");
    
    return $option;
}

function get_option_by_id($id) {
     global $dbp;
    $option = $dbp->get_row("SELECT * FROM options WHERE id = {$id} ");
    
    return $option;
}

function get_option_id($key) {
    $option = get_option_by_key($key);
    
    return $option->id;
}

function get_option_child($parent_id) {
    
    global $dbp;
    $options = $dbp->get_results("SELECT * FROM options WHERE parent = {$parent_id} ");
    
    return $options;
}



?>
